<?php

/**
 * user actions.
 *
 * @package    hybrid
 * @subpackage user
 * @author     long-nt
 */
class userActions extends sfActions
{
	/**
	* Executes list action
	*
	* @param sfRequest $objRequest A request object
	*/
	public function executeList(sfWebRequest $objRequest)
	{
		// get search parameters, consists of page and keyword
		$this->strParam = myCommonUtil::getSearchParameter($objRequest);

		// get keyword parameter
		$this->strKeyword = $objRequest->getParameter('keyword');

		$this->strUserIds = $objRequest->getParameter('strUserIds');

		return sfView::SUCCESS;
	}

	/**
	* Executes search action
	*
	* @param sfRequest $objRequest A request object
	*/
	public function executeSearch(sfWebRequest $objRequest)
	{
		// get search param, used for paging
		$objParam = $objRequest->getParameterHolder();
		$strParam = '';
		if ($objParam->get('keyword')) {
			$strParam .= '&keyword=' . $objParam->get('keyword');
		}

		if ($objRequest->getParameter('strUserIds')) {
			$strParam .= '&strUserIds=' . $objRequest->getParameter('strUserIds');
		}
		// get param, format: &page=x&keyword=y, used for going back from the others screen
		$strKeyword = $objRequest->getParameter('keyword');
		$strUserIds = $objRequest->getParameter('strUserIds');
		$page = $this->getRequestParameter('page', 1);
		$this->strUrlParam = substr(myCommonUtil::getSearchParameter($objRequest), 1);
		$this->strUrlParam = "&" . $this->strUrlParam;

		// get search data, init pager

		$this->objUserPager = new myArrayPager(null, myConstant::USER_MAX_RECORD_PER_PAGE);

		$this->objUserPager->setResultArray(UserTablePeer::getUserList($strKeyword, $strUserIds));
		$this->objUserPager->setPage($page);
		$this->objUserPager->setParameter('param', $strParam);
		$this->objUserPager->init();

		return sfView::SUCCESS;
	}

	/**
	* Executes detail action
	*
	* @param sfRequest $objRequest A request object
	*/
	public function executeDetail(sfWebRequest $objRequest)
	{
		$intUserId = $objRequest->getParameter('id');
		if (!myCommonUtil::checkIntegerInput($intUserId, true)) {
			$this->redirect('@staff');
		}

		$aryUser = UserTablePeer::getUserById($intUserId);
		if ($aryUser == null) {
			$this->redirect('@staff');
		}
		$this->strType = $objRequest->getParameter('type');
		$this->form = $this->setValueForUserForm($aryUser);

		$this->strUrlToBack = myCommonUtil::getParamBackToSearchScreen($objRequest);

		/* check floating window */
		if ($objRequest->getParameter('type') == "floating") {
			$this->setLayout("layoutBlank");
		}

		return sfView::SUCCESS;
	}

	/**
	* Executes edit action
	*
	* @param sfRequest $objRequest A request object
	*/
	public function executeEdit(sfWebRequest $objRequest)
	{

		$this->intUserId = null;
		$this->form = new UserForm(array(), array('strMode' => 'create'));
		$intUserId = $objRequest->getParameter('id');
		if ($objRequest->getMethod() != sfRequest::POST) {
			if (isset($intUserId)) {
				if (myCommonUtil::checkIntegerInput($intUserId, true)) {
					$aryUser = UserTablePeer::getUserById($intUserId);
					if ($aryUser == null) {
						$this->redirect('@user');
					}
					$this->form = $this->setValueForUserForm($aryUser, 'edit');
					$this->intUserId = $intUserId;
				} else {
					$this->redirect('@user');
				}
			}
		} else {
			$this->forward('user', 'save');
		}

		$this->strUrlToBack = myCommonUtil::getParamBackToSearchScreen($objRequest);

		/* check floating window */
		if ($objRequest->getParameter('type') == "floating") {
			$this->setLayout("layoutBlank");
		}

		return sfView::SUCCESS;
	}

	/**
	* Validates edit action
	*/
	public function validateEdit()
	{
		$objRequest = $this->getRequest();

		if ($objRequest->getMethod() == sfRequest::POST) {
			$aryUser = $objRequest->getParameter('aryUser');
			if ($aryUser['intId'] == null) {
				/* check email exist in database */
				if (!$objRequest->hasError('aryUser{txtMailAddress}')) {
					if (UserTablePeer::CheckExistLoginId($aryUser['txtLoginId']) == true) {
						$objRequest->setError('aryUser{txtLoginId}', 'Email khÃ´ng tá»“n táº¡i !');
					}
				}
				/* Check email confirm error */
				if ($objRequest->hasError('aryUser{txtLoginId}')) {
					$objRequest->removeError('aryUser{txtLoginIdConfirm}');
				}

			} else {
				$objRequest->removeError('aryUser{txtLoginId}');
				$objRequest->removeError('aryUser{txtLoginIdConfirm}');
			}
			if ($objRequest->hasErrors()) {
				return false;
			}
		}

		return true;
	}

	/**
	* Handles error edit action
	*
	* @param sfRequest $objRequest A request object
	*/
	public function handleErrorEdit()
	{
		$objRequest = $this->getRequest();
		$aryUser = $objRequest->getParameter('aryUser');

		$this->form = new UserForm($aryUser);

		$this->intUserId = $objRequest->getParameter('id');
		$this->strUrlToBack = myCommonUtil::getParamBackToSearchScreen($objRequest);

		return sfView::SUCCESS;
	}

	/**
	* Executes save action
	*
	* @param sfRequest $objRequest A request object
	*/
	public function executeSave(sfWebRequest $objRequest)
	{
		$aryUser = $objRequest->getParameter('aryUser');
		if ($aryUser == null) {
			$this->redirect('@user');
		}

		if ($aryUser['intId'] != null) {
			$objUser = UserTablePeer::retrieveByPK($aryUser['intId']);

			if ($aryUser['txtName'] != '') {
				$objUser->setName($aryUser['txtName']);
			} else {
				$objUser->setName(null);
			}
			if ($aryUser['txtAddress'] != '') {
				$objUser->setAddress($aryUser['txtAddress']);
			} else {
				$objUser->setAddress(null);
			}
			if ($aryUser['radIsAdmin'] == '1'){
				$objUser->setIsAdmin(true);
			} else {
				$objUser->setIsAdmin(false);
			}

			$objUser->setUpdateUserId($this->getUser()->getTUserId());
			$objUser->save();

			$strUrlToBack = myCommonUtil::getParamBackToSearchScreen($objRequest);
			$this->redirect('@user' . $strUrlToBack);
		} else {
			$objUser = new UserTable();
			$objUser->setLoginId($aryUser['txtLoginId']);

			if ($aryUser['radIsAdmin'] == '1'){
				$objUser->setIsAdmin(true);
			} else {
				$objUser->setIsAdmin(false);
			}
			$objUser->save();
			$this->forward('mail', 'mailSettingPassword');
		}

		return sfView::SUCCESS;
	}

	/**
	 * Set value to form for displaying
	 *
	 * @param array $aryUser
	 * @return UserForm
	 */
	private function setValueForUserForm($aryUser, $strMode = 'create')
	{
		$form = new UserForm();
		$form->setDefaults(array(
			'intId' => $aryUser['id'],
			'txtName' => $aryUser['name'],
			'txtBirthDay' => $aryUser['birth_day'],
			'txtLoginId' => $aryUser['mail_address'],
			'txtAddress' => $aryUser['address'],
			'strMode' => $strMode,
		));

		if ($aryUser['is_male'] == true) {
			$form->setDefault('radIsMale', 1);
		} else if (!is_null($aryUser['is_male'])) {
			$form->setDefault('radIsMale', 2);
		}
		if (array_key_exists('is_admin', $aryUser)) {
			if ($aryUser['is_admin'] == true) {
				$form->setDefault('radIsAdmin', 1);
			} else if (!is_null($aryUser['is_admin'])) {
				$form->setDefault('radIsAdmin', 2);
			}
		}

		return $form;
	}
	
	
	/**
	 * Executes lostPassword action
	 *
	 * @param sfRequest $request A request object
	 */
	public function executeLostPassword(sfWebRequest $objRequest)
	{
		if ($objRequest->getMethod('post') == sfRequest::POST) {
			$strLoginId = $objRequest->getParameter('txtMailAddress');
			$this->forward("mail", "mailLostPassword");
		}

		return sfView::SUCCESS;
	}

	/**
	 * Validates lost password action
	 */
	public function validateLostPassword()
	{
		$objRequest = $this->getRequest();
		if ($objRequest->getMethod() == sfRequest::POST) {
			$strLoginId = $objRequest->getParameter('txtMailAddress');
			if (!$objRequest->hasError('txtMailAddress')) {
				$aryUser = UserTablePeer::getUserByLoginId($strLoginId);
				if (is_null($aryUser)) {
					$objRequest->setError("txtMailAddress", 'User Không Tồn Tại !');
				}
			}
			if ($objRequest->hasErrors()) {
				return false;
			}
		}
		return true;
	}

	/**
	 * Handles error lost password action
	 *
	 * @param sfRequest $objRequest A request object
	 */
	public function handleErrorLostPassword()
	{
		return sfView::SUCCESS;
	}

	
	
}
